package com.czmh.etc.onlinepubsys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.czmh.etc.onlinepubsys.messagehandler.ScanEventMessageHander;
import com.czmh.etc.onlinepubsys.messagehandler.SubscribeEventMessageHander;
import com.czmh.etc.onlinepubsys.messagehandler.UnsubscribeEventMessageHander;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 
 * @描述： 
 * @author： Seven.
 * @创建日期：Nov 13, 2019 7:35:42 PM. 
 * @版本：1.0.0
 */
@Controller
@Slf4j
@RequestMapping("/wechat.html")
@Api(tags = "微信访问控制器")
public class WxIndexController {
    @Autowired
    private WxMpService                   wxMpService;
    @Autowired
    private WxMpMessageRouter             router;
    @Autowired
    private SubscribeEventMessageHander   subscribeEventMessageHander;
    @Autowired
    private UnsubscribeEventMessageHander unsubscribeEventMessageHander;
    @Autowired
    private ScanEventMessageHander        scanEventMessageHander;

    @RequestMapping(method = RequestMethod.GET)
    public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        String signature = request.getParameter("signature");
        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");
        log.info("请求参数===signature:" + signature + "  timestamp:" + timestamp + "  nonce:" + nonce);
        String echostr = request.getParameter("echostr");
        log.info("echostr===:" + echostr);
        if (wxMpService.checkSignature(timestamp, nonce, signature)) {
            response.getWriter().println(echostr);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public void messageRouter(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        WxMpXmlMessage message = WxMpXmlMessage.fromXml(request.getInputStream());
        log.info("微信路由的消息内容为：" + message);
        router
                // 处理关注事件
                .rule().async(false)// 设置为同步回复
                .msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE)
                .handler(subscribeEventMessageHander).end()
                // 处理取消关注事件
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unsubscribeEventMessageHander).end()
                // 处理扫描事件
                .rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SCAN)
                .handler(scanEventMessageHander).end();
        WxMpXmlOutMessage reMsg = router.route(message);
        log.info("回复的消息：" + reMsg);
        if (reMsg != null) {
            // 说明是同步回复的消息
            String xml = reMsg.toXml();
            log.info("回复的消息文本xml：" + xml);
            response.getWriter().write(xml);
        }
    }
}